package bottle.jack;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;

@Entity
public class Message {

	@Id
	 public String id;
	
	@Embedded
	public Content messageData;
	@Embedded
	public String destination;
	@Embedded
	public String rootSender;
	@Embedded
	public String currentMessageHolder;
	@Embedded
	public Location startLocation;
	@Embedded
	public ArrayList<Annotation> annotations;
	@Embedded
	public int iterationsLeft;
	@Embedded
	public ArrayList<String> annotationText;
	/**
	 * Converts the message data to a JSONObject.
	 * @return the JSON object.
	 */
  public final JSONObject toJSON() {
    JSONObject jsonObj = new JSONObject();
    jsonObj.put("ID", id);
    jsonObj.put("messageData", messageData);
    if (iterationsLeft == 0) {
      jsonObj.put("final", true);
    }
    jsonObj.put("latitude", startLocation.latitude);
    jsonObj.put("longitude", startLocation.longitude);
    jsonObj.put("accuracy", startLocation.accuracy);
    jsonObj.put("date", startLocation.date);
    jsonObj.put("name", rootSender);
    JSONArray annotationJson = new JSONArray();
    for (Annotation annotation : annotations) {
      annotationJson.put(annotation.toJSON());
    }
    jsonObj.put("annotations", annotationJson);
    return jsonObj;
  }

}
